package org.dream.ui.chart.impl;

import java.awt.Color;
import java.awt.Graphics;
import java.util.List;

import org.dream.domain.Daydeal;
import org.dream.ui.chart.Chart;
import org.dream.ui.chart.Plot;

/**
 * 价格K线
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class VolPlot extends Plot {
	List<Daydeal> daydealList;

	public VolPlot(List<Daydeal> daydealList, Chart chart, int index) {
		super(chart, index);
		this.daydealList = daydealList;

	}

	@Override
	public Double getMaxY() {
		Double maxY = -Double.MAX_VALUE;
		for (int i = MinX; i <= maxX && i < daydealList.size(); i++) {
			Daydeal daydeal = daydealList.get(i);
			if (maxY == null || daydeal.getVOL().compareTo(maxY) > 0) {
				maxY = daydeal.getVOL();
			}
		}
		return maxY * 1.25;
	}

	@Override
	public Double getMinY() {
		Double minY = Double.MAX_VALUE;
		for (int i = MinX; i <= maxX && i < daydealList.size(); i++) {
			Daydeal daydeal = daydealList.get(i);
			if (minY == null || daydeal.getVOL().compareTo(minY) < 0) {
				minY = daydeal.getVOL();
			}
		}
		return minY;
	}

	@Override
	public void draw(Graphics g) {

		int min = this.chart.getYAxis(index).getYAxisModel().getPixelYButtom();
		for (int i = this.chart.getXAxis().getXAxisModel().getMinValue() - 1; i < this.chart.getXAxis().getXAxisModel()
				.getMaxValue(); i++) {
			Daydeal daydeal = daydealList.get(i);
			int dayXPixel = chart.getXCoordinatePixel(new Double(i + 1));
			int turnOver = chart.getYCoordinatePixel(daydeal.getVOL(), index);
			// 58:18
			int halfWidth = (int) (chart.getXAxis().getXAxisModel().getTickSpacingPixel() / (58 + 18) * 58 / 2);

			if (daydeal.getOPEN() <= daydeal.getCLOSE()) {
				g.setColor(Color.RED);
				g.drawRect(dayXPixel - halfWidth, turnOver, halfWidth * 2, Math.abs(turnOver - min));
			} else {
				g.setColor(new Color(84, 255, 255));
				g.drawRect(dayXPixel - halfWidth, turnOver, halfWidth * 2, Math.abs(turnOver - min));
				g.fillRect(dayXPixel - halfWidth, turnOver, halfWidth * 2, Math.abs(turnOver - min));
			}

		}

	}
}
